#include <ctime>
#include <sys/types.h>
#include <unistd.h>
#include "BlockQueue.hpp"

void* consumer(void* args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int>*>(args);
    while(true)
    {
        int data = 0;
        bq->take(&data);
        std::cout << "take: " << data << std::endl;
    }

    return nullptr;
}

void* productor(void* args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int>*>(args);
    while(true)
    {
        int data = rand() % 10 + 1;
        bq->put(data);
        std::cout << "put: " << data << std::endl;
        sleep(1);
    }

    return nullptr;
}

int main()
{
    srand((uint16_t)time(nullptr) ^ pthread_self() ^ getpid()); //用三个随机数，可以生成更随机的数据

    BlockQueue<int>* bq = new BlockQueue<int>();

    pthread_t c, p;
    // pthread_t c[5], p[5]; //多生产多消费

    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&p, nullptr, productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bq;
    return 0;
}